<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="generator" content="VuePress 2.0.0-beta.63">
    <style>
      :root {
        --c-bg: #fff;
      }
      html.dark {
        --c-bg: #22272e;
      }
      html, body {
        background-color: var(--c-bg);
      }
    </style>
    <script>
      const userMode = localStorage.getItem('vuepress-color-scheme');
			const systemDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
			if (userMode === 'dark' || (userMode !== 'light' && systemDarkMode)) {
				document.documentElement.classList.toggle('dark', true);
			}
    </script>
    <link rel="icon" href="/img/java.jpg"><title>第十二章 | Java课程</title><meta name="description" content="个人博客">
    <link rel="preload" href="/javaFoundation/assets/style-fce9e9a8.css" as="style"><link rel="stylesheet" href="/javaFoundation/assets/style-fce9e9a8.css">
    <link rel="modulepreload" href="/javaFoundation/assets/app-9add14af.js"><link rel="modulepreload" href="/javaFoundation/assets/java12.html-6cc69e7d.js"><link rel="modulepreload" href="/javaFoundation/assets/java12.html-59387421.js"><link rel="prefetch" href="/javaFoundation/assets/index.html-96c7a138.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java1.html-55711407.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java11.html-14d0198c.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java13.html-b16483f7.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java14.html-3a207f62.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java15.html-dd025587.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java2.html-08c0085f.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java3.html-8a80b644.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java4.html-91989e4b.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java5.html-93861bfd.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java6.html-6615830e.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java7.html-00318e66.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java8.html-7db1538d.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/index.html-53893ddf.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/404.html-60b35caa.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/index.html-c8564237.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java1.html-fbde0086.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java11.html-25f94899.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java13.html-d2d1b56b.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java14.html-d1a1466f.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java15.html-19a3e953.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java2.html-10032c3d.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java3.html-381ca5d5.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java4.html-1655b315.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java5.html-db3e5151.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java6.html-9903f3bf.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java7.html-a9db2489.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/java8.html-4c43e3d1.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/index.html-477bdcf2.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/404.html-3648c52f.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/style-e9220a04.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/docsearch-1d421ddb.js" as="script"><link rel="prefetch" href="/javaFoundation/assets/index-99a3937c.js" as="script">
  </head>
  <body>
    <div id="app"><!--[--><div class="theme-container"><!--[--><header class="navbar"><div class="toggle-sidebar-button" title="toggle sidebar" aria-expanded="false" role="button" tabindex="0"><div class="icon" aria-hidden="true"><span></span><span></span><span></span></div></div><span><a href="/javaFoundation/" class=""><img class="logo" src="/javaFoundation/img/java.jpg" alt="Java课程"><span class="site-name can-hide">Java课程</span></a></span><div class="navbar-items-wrapper" style=""><!--[--><!--]--><nav class="navbar-items can-hide"><!--[--><div class="navbar-item"><a href="/javaFoundation/" class="" aria-label="首页"><!--[--><!--]--> 首页 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/javaFoundation/javaFoundation/" class="" aria-label="Java基础"><!--[--><!--]--> Java基础 <!--[--><!--]--></a></div><div class="navbar-item"><a class="external-link" href="http://github.com/nodeing666" rel="noopener noreferrer" target="_blank" aria-label="github"><!--[--><!--]--> github <span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span><!--[--><!--]--></a></div><!--]--></nav><!--[--><!--]--><button class="toggle-color-mode-button" title="toggle color mode"><svg style="" class="icon" focusable="false" viewBox="0 0 32 32"><path d="M16 12.005a4 4 0 1 1-4 4a4.005 4.005 0 0 1 4-4m0-2a6 6 0 1 0 6 6a6 6 0 0 0-6-6z" fill="currentColor"></path><path d="M5.394 6.813l1.414-1.415l3.506 3.506L8.9 10.318z" fill="currentColor"></path><path d="M2 15.005h5v2H2z" fill="currentColor"></path><path d="M5.394 25.197L8.9 21.691l1.414 1.415l-3.506 3.505z" fill="currentColor"></path><path d="M15 25.005h2v5h-2z" fill="currentColor"></path><path d="M21.687 23.106l1.414-1.415l3.506 3.506l-1.414 1.414z" fill="currentColor"></path><path d="M25 15.005h5v2h-5z" fill="currentColor"></path><path d="M21.687 8.904l3.506-3.506l1.414 1.415l-3.506 3.505z" fill="currentColor"></path><path d="M15 2.005h2v5h-2z" fill="currentColor"></path></svg><svg style="display:none;" class="icon" focusable="false" viewBox="0 0 32 32"><path d="M13.502 5.414a15.075 15.075 0 0 0 11.594 18.194a11.113 11.113 0 0 1-7.975 3.39c-.138 0-.278.005-.418 0a11.094 11.094 0 0 1-3.2-21.584M14.98 3a1.002 1.002 0 0 0-.175.016a13.096 13.096 0 0 0 1.825 25.981c.164.006.328 0 .49 0a13.072 13.072 0 0 0 10.703-5.555a1.01 1.01 0 0 0-.783-1.565A13.08 13.08 0 0 1 15.89 4.38A1.015 1.015 0 0 0 14.98 3z" fill="currentColor"></path></svg></button><!--[--><div id="docsearch-container" style="display:none;"></div><div><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><svg width="20" height="20" class="DocSearch-Search-Icon" viewBox="0 0 20 20"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"><svg width="15" height="15" class="DocSearch-Control-Key-Icon"><path d="M4.505 4.496h2M5.505 5.496v5M8.216 4.496l.055 5.993M10 7.5c.333.333.5.667.5 1v2M12.326 4.5v5.996M8.384 4.496c1.674 0 2.116 0 2.116 1.5s-.442 1.5-2.116 1.5M3.205 9.303c-.09.448-.277 1.21-1.241 1.203C1 10.5.5 9.513.5 8V7c0-1.57.5-2.5 1.464-2.494.964.006 1.134.598 1.24 1.342M12.553 10.5h1.953" stroke-width="1.2" stroke="currentColor" fill="none" stroke-linecap="square"></path></svg></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div></header><!--]--><div class="sidebar-mask"></div><!--[--><aside class="sidebar"><nav class="navbar-items"><!--[--><div class="navbar-item"><a href="/javaFoundation/" class="" aria-label="首页"><!--[--><!--]--> 首页 <!--[--><!--]--></a></div><div class="navbar-item"><a href="/javaFoundation/javaFoundation/" class="" aria-label="Java基础"><!--[--><!--]--> Java基础 <!--[--><!--]--></a></div><div class="navbar-item"><a class="external-link" href="http://github.com/nodeing666" rel="noopener noreferrer" target="_blank" aria-label="github"><!--[--><!--]--> github <span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span><!--[--><!--]--></a></div><!--]--></nav><!--[--><!--]--><ul class="sidebar-items"><!--[--><li><p tabindex="0" class="sidebar-item sidebar-heading active">java基础 <!----></p><ul style="" class="sidebar-item-children"><!--[--><li><a href="/javaFoundation/JavaFoundation/java1.html" class="sidebar-item" aria-label="第一章"><!--[--><!--]--> 第一章 <!--[--><!--]--></a><!----></li><li><a href="/javaFoundation/JavaFoundation/java2.html" class="sidebar-item" aria-label="第二章"><!--[--><!--]--> 第二章 <!--[--><!--]--></a><!----></li><li><a href="/javaFoundation/JavaFoundation/java3.html" class="sidebar-item" aria-label="第三章"><!--[--><!--]--> 第三章 <!--[--><!--]--></a><!----></li><li><a href="/javaFoundation/JavaFoundation/java4.html" class="sidebar-item" aria-label="第四章"><!--[--><!--]--> 第四章 <!--[--><!--]--></a><!----></li><li><a href="/javaFoundation/JavaFoundation/java5.html" class="sidebar-item" aria-label="第五章"><!--[--><!--]--> 第五章 <!--[--><!--]--></a><!----></li><li><a href="/javaFoundation/JavaFoundation/java6.html" class="sidebar-item" aria-label="第六章"><!--[--><!--]--> 第六章 <!--[--><!--]--></a><!----></li><li><a href="/javaFoundation/JavaFoundation/java7.html" class="sidebar-item" aria-label="第七章"><!--[--><!--]--> 第七章 <!--[--><!--]--></a><!----></li><li><a href="/javaFoundation/JavaFoundation/java8.html" class="sidebar-item" aria-label="第八章"><!--[--><!--]--> 第八章 <!--[--><!--]--></a><!----></li><li><a href="/javaFoundation/JavaFoundation/java11.html" class="sidebar-item" aria-label="第十一章"><!--[--><!--]--> 第十一章 <!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/javaFoundation/JavaFoundation/java12.html" class="router-link-active router-link-exact-active router-link-active sidebar-item active" aria-label="第十二章"><!--[--><!--]--> 第十二章 <!--[--><!--]--></a><!----></li><li><a href="/javaFoundation/JavaFoundation/java13.html" class="sidebar-item" aria-label="第十三章"><!--[--><!--]--> 第十三章 <!--[--><!--]--></a><!----></li><li><a href="/javaFoundation/JavaFoundation/java14.html" class="sidebar-item" aria-label="第十四章"><!--[--><!--]--> 第十四章 <!--[--><!--]--></a><!----></li><li><a href="/javaFoundation/JavaFoundation/java15.html" class="sidebar-item" aria-label="第十五章"><!--[--><!--]--> 第十五章 <!--[--><!--]--></a><!----></li><!--]--></ul></li><!--]--></ul><!--[--><!--]--></aside><!--]--><!--[--><main class="page"><!--[--><!--]--><div class="theme-default-content"><!--[--><!--]--><div><h1 id="第十二章" tabindex="-1"><a class="header-anchor" href="#第十二章" aria-hidden="true">#</a> 第十二章</h1><h4 id="会定义和使用类的方法" tabindex="-1"><a class="header-anchor" href="#会定义和使用类的方法" aria-hidden="true">#</a> 会定义和使用类的方法</h4><h5 id="理解变量作用域" tabindex="-1"><a class="header-anchor" href="#理解变量作用域" aria-hidden="true">#</a> 理解变量作用域</h5><div class="custom-container tip"><p class="custom-container-title">提示</p><p>会添加JavaDoc注释</p></div><p>1.定义类的方法 步骤一：定义方法名以及返回值类型</p><p>步骤二：编写方法体</p><div class="language-java line-numbers-mode" data-ext="java"><pre class="language-java"><code><span class="token keyword">public</span> 返回值类型 方法名<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token punctuation">{</span>
    <span class="token comment">//方法的主体</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>2.方法的返回值两种情况 1）如果方法具有返回值，方法中必须使用关键字return返回该值，返回值类型为该返回值的类型</p><p>2）如果方法没有返回值，返回值类型为void</p><p>3.语法 return 表达式; 作用： 跳出方法、返回结果</p><p>4.调用带返回值方法的方式：<span class="badge danger" style=""><!--[-->重点<!--]--></span></p><div class="language-java line-numbers-mode" data-ext="java"><pre class="language-java"><code><span class="token number">1</span>）类型 变量名<span class="token operator">=</span>对象名<span class="token punctuation">.</span>方法<span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>说明：变量的类型一定要与方法的返回类型一样</p><p>2）在输出的代码中调用带返回值类型的方法：</p><div class="language-java line-numbers-mode" data-ext="java"><pre class="language-java"><code><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>对象名<span class="token punctuation">.</span>方法名<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>5.对于没有返回值类型(void)的方法，需要直接调用.</p><div class="language-java line-numbers-mode" data-ext="java"><pre class="language-java"><code>如：对象名<span class="token punctuation">.</span>方法名<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>没有返回值类型的方法，不能在输出代码中调用</p><div class="custom-container warning"><p class="custom-container-title">注意事项</p><p>方法中可以调用方法，但是一个方法定义中，不能再包括另外一个方法定义。</p><p>方法的返回类型只有一个，方法的返回值也之呢个有一个</p></div><p>成员变量个局部变量的区别 1.作用域不同</p><p>1）局部变量的作用域仅限于定义它的方法</p><p>2）成员变量的作用域在整个类内部都是可见的</p><p>2.初始值不同</p><p>1）Java会给成员变量一个初始值</p><p>2）java不会给局部变量赋予初始值</p><div class="custom-container warning"><p class="custom-container-title">注意</p><p>1）在同一个方法中，不允许有同名局部变量</p><p>2）在不同的方法中，可以有同名局部变量</p><p>3）在同一个类中，成员变量和局部变量同名时，局部变量具有更高的优先级</p></div><blockquote><p>减少代码量实现代码复用</p></blockquote></div><!--[--><!--]--></div><footer class="page-meta"><!----><!----><!----></footer><nav class="page-nav"><p class="inner"><span class="prev"><a href="/javaFoundation/JavaFoundation/java11.html" class="" aria-label="第十一章"><!--[--><!--]--> 第十一章 <!--[--><!--]--></a></span><span class="next"><a href="/javaFoundation/JavaFoundation/java13.html" class="" aria-label="第十三章"><!--[--><!--]--> 第十三章 <!--[--><!--]--></a></span></p></nav><!--[--><!--]--></main><!--]--></div><!----><!--]--></div>
    <script type="module" src="/javaFoundation/assets/app-9add14af.js" defer></script>
  </body>
</html>
